package com.jinxin.platform.cdelecinspect.controller;

import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.base.common.pojo.InvokeCfg;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.base.common.support.BusinessDelegate;
import com.jinxin.platform.cdelecinspect.constant.Constants;
import com.jinxin.platform.cdelecinspect.domain.SysUserVO;
import com.jinxin.platform.cdelecinspect.mapper.EleinspectTaskMapper;
import com.jinxin.platform.cdelecinspect.mapper.pojo.EleinspectTask;
import com.jinxin.platform.cdelecinspect.result.ResultEntity;
import com.jinxin.platform.cdelecinspect.result.ResultTool;
import com.jinxin.platform.cdelecinspect.rpc.SysUserRpc;
import com.jinxin.platform.cdelecinspect.service.EleinspectLineManageService;
import com.jinxin.platform.cdelecinspect.service.EleinspectTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-04-06
 * @Author: Lingnan
 **/
@Slf4j
@RestController
@RequestMapping("/insTask")
public class EleinspectTaskController {
    @Autowired
    private EleinspectTaskService taskService;
    @Autowired
    private EleinspectTaskMapper taskMapper;
    @Autowired
    private EleinspectLineManageService lineManageService;
    @Autowired
    private BusinessDelegate businessDelegate;
    @Autowired
    private SysUserRpc sysUserRpc;


    @RequestMapping("/listByCondition")
    public Map<String,Object> listByCondition(String staff, String line, String startTime,String endTime, @RequestParam long page, @RequestParam long limit){
        Map<String,Object> resultMap = new HashMap<>();
        HashMap<String,Object> param = new HashMap<>();
        param.put("staff", staff);
        param.put("lineId", line);
        param.put("startTime", startTime);
        param.put("endTime", endTime);
        param.put("page", page);
        param.put("limit", limit);
        resultMap.put("code",0);
        resultMap.put("msg","");
        List<Map<String,Object>> list = taskService.listTasksPage(param);
        resultMap.put("count",taskService.listTasksPageCount(param));
        resultMap.put("data",list);
        return resultMap;
    }

    @RequestMapping("/listForApp")
    public Map<String,Object> listForApp(String startTime,String endTime, @RequestParam long page,
                                         @RequestParam long limit){
        SysUserVO currentUser = sysUserRpc.getCurrentUser();
        if (currentUser == null) {
            log.error("未登录--,insTask listForApp接口");
            return null;
        }
        log.info("巡检任务当前登录者: " + currentUser);
        Map<String,Object> resultMap = new HashMap<>();
        HashMap<String,Object> param = new HashMap<>();
        param.put("staffId", currentUser.getUserId());
        param.put("startTime", startTime);
        param.put("endTime", endTime);
        param.put("page", page);
        param.put("limit", limit);
        resultMap.put("code",0);
        resultMap.put("msg","");
        log.info("查询参数 : " + param.toString());
        List<Map<String,Object>> list = taskService.listTasksPage(param);
        log.info("巡检任务 : " + list.toString());
        resultMap.put("count",taskService.listTasksPageCount(param));
        resultMap.put("data",list);
        return resultMap;
    }

    @PostMapping(value = "/insertBatch")
    public ResultEntity insertBatch(@RequestBody EleinspectTask[] taskArray){
        List<EleinspectTask> taskList = new ArrayList<>();
        for(EleinspectTask task : taskArray){
            taskList.add(task);
        }
        int num = taskService.insertBatch(taskList);
        if(num > 0){
            return ResultTool.ok("success");
        }
        return ResultTool.fail("fail");
    }

    @RequestMapping(value = "/modifyTask", method = RequestMethod.POST)
    public ResultEntity modifyTask(String id){
        if(!StringUtils.isEmpty(id)){
            EleinspectTask task = taskMapper.selectByKey(id);
            if(task != null){
                task.setStatus(Constants.TASK_STATUS_CANCELED);
                int num = taskMapper.updateByKey(task);
                if(num > 0){
                    return ResultTool.ok(String.format("Modify task %s success.", id));
                }
                return ResultTool.ok(String.format("Modify task %s fail.", id));
            }
            return ResultTool.fail(String.format("Task (id:%s) is inexistent.", id));
        }
        log.error(String.format("Params fail, taskId %s is inexistent.", id));
        return ResultTool.fail(String.format("Params fail, taskId %s is inexistent.", id));
    }

    @RequestMapping(value = "/updateTask", method = RequestMethod.POST)
    public ResultEntity updateTask(EleinspectTask task){
        if(task != null){
            int num = taskService.updateByKey(task);
            if(num > 0){
                return ResultTool.ok(String.format("Modify task %s success.", task.getId()));
            }
            return ResultTool.ok(String.format("Modify task %s fail.", task.getId()));
        }
        log.error("Params fail, taskId is inexistent.");
        return ResultTool.fail("Params fail, taskId is inexistent.");
    }

    @RequestMapping(value = "/deleteTask", method = RequestMethod.GET)
    public ResultEntity deleteTask(String id){
        if(!StringUtils.isEmpty(id)){
            int num = taskMapper.deleteByKey(id);
            if(num > 0){
                return ResultTool.ok(String.format("Delete task (id:%s) success.", id));
            }
            return ResultTool.ok(String.format("Delete task (id:%s) fail.", id));
        }
        log.error(String.format("Params fail, taskId %s is inexistent.", id));
        return ResultTool.fail(String.format("Params fail, taskId %s is inexistent.", id));
    }

    @RequestMapping("/listPointItems")
    public Map<String,Object> listPointItems(String taskId, String lineId){
        Map<String,Object> resultMap = new HashMap<>();
        HashMap<String,Object> param = new HashMap<>();
        param.put("taskId", taskId);
        param.put("lineId", lineId);
        resultMap.put("code",0);
        resultMap.put("msg","");
        resultMap.put("data",taskMapper.listPointItemsResult(param));
        return resultMap;
    }
    @GetMapping("/getLineInfo")
    public ResultEntity getLineInfo(String id) {
        if(!StringUtils.isEmpty(id)){
            return ResultTool.ok(lineManageService.selectByKey(id));
        }
        log.error("Params fail, lineId is inexistent.");
        return ResultTool.fail("Param error.");
    }

    @RequestMapping("/getStaffList")
    public JsonResult getStaffList() {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey("staff");
        cfg.setClassName("StaffRpcService");
        cfg.setFunName("getStaffList");
        HashMap<String, String> map = new HashMap<>();
//            map.put("type", type);
        cfg.setArgs(map);
        JsonResult result = businessDelegate.invoke(cfg);
        if(result.getSuccess()){
            log.info(result.toString());
        }
//        else{
//            OptionItem item1 = new OptionItem("1","人员1");
//            OptionItem item2 = new OptionItem("2","人员2");
//            List<OptionItem> list = new ArrayList<>();
//            list.add(item1);
//            list.add(item2);
//            return JsonResult.ok(list);
//        }
        return result;
    }

    //app接口-开始巡更任务
    @RequestMapping("/doTask")
    public JsonResult doTask(String taskId) {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey("app");
        cfg.setClassName("EleinspectTaskRpcService");
        cfg.setFunName("getEleinspectTask");
        HashMap<String, String> map = new HashMap<>();
        map.put("taskId", taskId);
        cfg.setArgs(map);
        JsonResult result = businessDelegate.invoke(cfg);
        if(result.getSuccess()){
            JSONObject jsonObject = JSONObject.parseObject(result.getData().toString());
            int taskStatus = Integer.parseInt(String.valueOf(jsonObject.get("taskStatus")));
            String taskStartTime = String.valueOf(jsonObject.get("taskStartTime"));
            EleinspectTask eleinspectTask = taskMapper.selectByKey(taskId);
            eleinspectTask.setInspectStartTime(taskStartTime);
            eleinspectTask.setStatus(taskStatus);
            taskMapper.updateByKey(eleinspectTask);
            log.info(result.toString());
        }
        return result;
    }
}
